home *** CD-ROM | disk | FTP | other *** search
/ 9-Digit Zip Code Directory / 9-Digit Zip Code Directory (American Business Information) (ABIZIP-12).ISO / z4src.zip / BSPSIZE.C < prev    next >
C/C++ Source or Header  |  1993-08-13  |  5KB  |  177 lines

  1. //----------------------------------------------------------------------------
  2. //                            MODULE DESCRIPTION
  3. //
  4. //  Module:    bspsize.c
  5. //   Title:    Base library
  6. //  Notice:    John M. Weeder
  7. //                 Copyright (c) 1993. All rights reserved.
  8. //             This module contains proprietary information and should be 
  9. //                treated as confidential.
  10. //
  11. //----------------------------------------------------------------------------
  12. //                           MAINTENANCE HISTORY
  13. //
  14. // $Workfile$
  15. // $Revision$
  16. //   $Author$
  17. //     $Date$
  18. //      $Log$    
  19. //
  20. //----------------------------------------------------------------------------
  21. //                             MODULE NARRATIVE
  22. //
  23. //
  24. //    This module contains code to determine the memory requirement of an
  25. //    active DOS mode program. This function does nothing on other operating
  26. //    systems.
  27. //
  28. //    The code in this module should be written entirely in C. 
  29. //    Do not use any C++ constructs.
  30. //
  31. //    This module is portable to:
  32. //        DOS 3.X+
  33. //        MS Windows 3.X+
  34. //        OS/2 2.X+
  35. //        OS/2 2.0 PM
  36. //        SCO UNIX.
  37. //
  38. //    The following compilers are supported:
  39. //        MSC 6.0A
  40. //        MSC/C++ 7.0
  41. //        Borland C++ 3.1 for DOS
  42. //        Borland C++ 1.0 for OS/2 2.X
  43. //        SCO UNIX cc
  44. //
  45. //----------------------------------------------------------------------------
  46. #include <bs.h>
  47.  
  48. //----------------------------------------------------------------------------
  49. //    Data types
  50. //----------------------------------------------------------------------------
  51. #if OS_DOS && COMPILER_BORLAND
  52. #pragma option -a-
  53. typedef struct                                 // Structure defining format of
  54. {                                                    // DOS MCB
  55.     CHAR        chType;                            // Type 'M' or 'Z'
  56.     USHORT    usOwner;                            // Owner segment
  57.     USHORT    usParas;                            // Number of paragraphs
  58. } _FAR_ *PMCB;
  59. #endif
  60.  
  61.  
  62. //----------------------------------------------------------------------------
  63. //   Description:    Compute program size and other memory usage data.
  64. //                          These values are only meaningful under dos. Under
  65. //                        other OS's, dummy values are returned.
  66. //    Parameters:    plProgram        Pointer to variable to recieve total bytes
  67. //                                            used by current program 
  68. //                   plFree            Pointer to variable to recieve total bytes
  69. //                                            free.
  70. //       Returns:
  71. //----------------------------------------------------------------------------
  72. VOID FN_E ProgramSize(PLONG plProgram, PLONG plFree)
  73. {
  74. #if OS_DOS && COMPILER_BORLAND
  75.     USHORT usMcbSeg;
  76.     LONG lFree = 0;
  77.     LONG lProgram = 0;
  78.     PMCB pmcb;
  79.  
  80.     _asm {                                         // Get segment of first MCB
  81.         mov     ah, 52h                            // from DOS 'invars'
  82.         int    21h
  83.  
  84.         sub    bx, 2
  85.         mov    ax, es:[bx]
  86.         mov    [usMcbSeg], ax
  87.         }
  88.  
  89.     do                                                // Search MCB chain for first child
  90.         {
  91.         pmcb = (PMCB)(((ULONG)usMcbSeg) << 16);
  92.         if (pmcb->usOwner == _psp)
  93.             {
  94.             lProgram += pmcb->usParas;
  95.             }
  96.         else if (!pmcb->usOwner)
  97.             {
  98.             lFree += pmcb->usParas;
  99.             }
  100.         usMcbSeg += pmcb->usParas + 1;    // Skip to next
  101.         }
  102.     while (pmcb->chType == 'M');
  103.  
  104.     lProgram *= 16;
  105.     lFree *= 16;
  106.     if (plProgram)
  107.         plProgram[0] = lProgram;
  108.     if (plFree)
  109.         plFree[0] = lFree;
  110. #else
  111.     if (plProgram)
  112.         plProgram[0] = 1L;                    // 1 byte (token value)
  113.     if (plFree)
  114.         plFree[0] = 1024L * 1024L * 32L;    // 32 Mb
  115. #endif
  116.     return ;
  117. }
  118.  
  119.  
  120. //----------------------------------------------------------------------------
  121. //   Description:    Compute program size and other memory usage data.
  122. //                          Write this information to a file!
  123. //    Parameters:
  124. //       Returns:
  125. //----------------------------------------------------------------------------
  126. VOID FN_E ProgramSizeShow(PCSZ pszLocation)
  127. {
  128.     FILE *file;
  129.     LONG lFree;
  130.     LONG lProgram;
  131.  
  132.     if (!LogEnabled())
  133.         return ;
  134.     ProgramSize(&lProgram, &lFree);
  135.     if ((file = fopen("size.log", "a+t")) == NULL)
  136.         return ;
  137.  
  138.     fprintf(file,
  139.         "Program size %s.\n"
  140.         "    Program: %8ld bytes  (%6ld K)\n"
  141.         "     Unused: %8ld bytes  (%6ld K)\n"
  142.         "             --------------  -----------\n"
  143.         "  Available: %8ld bytes  (%6ld K)\n\n",
  144.         pszLocation,
  145.         lProgram, (long)K(lProgram),
  146.         lFree, (long)K(lFree),
  147.         lFree+lProgram, (long)K(lFree + lProgram));
  148.  
  149.     fclose(file);
  150.     return ;
  151. }
  152.  
  153.  
  154. //----------------------------------------------------------------------------
  155. //   Description:    Run standard test suite
  156. //    Parameters:    sTest        Test to run.
  157. //                                        0        Run all default tests (except).
  158. //       Returns:    TRUE if successful.
  159. //----------------------------------------------------------------------------
  160. #if COMPILE_TEST
  161. BOOL FN ProgSizeTest(SHORT sTest)
  162. {
  163.     LONG lFree;
  164.     LONG lProgram;
  165.  
  166.     NOTUSED(sTest);
  167.     ProgramSize(&lProgram, &lFree);
  168.     Output("%ld bytes used.\n%ld bytes free. %ld total.\n",
  169.         lProgram, lFree, lProgram+lFree);
  170.     ProgramSizeShow("in test.");
  171.     return TRUE;
  172. }
  173. #endif
  174. //----------------------------------------------------------------------------
  175. //------------------------------- End of File --------------------------------
  176. //----------------------------------------------------------------------------
  177.